﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace RouteTiming.SLClient.DynamicModel
{
    public class DynamicBaseEntity : INotifyPropertyChanged
    {
        /// <summary>
        /// A tool for getting the property name from Expression
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="propertyExpression"></param>
        /// <returns></returns>
        private static string GetPropertyName<T>(Expression<Func<T>> propertyExpression)
        {
            if (propertyExpression == null)
            {
                throw new ArgumentException("Property expression not specified!");
            }

            var memberExpression = propertyExpression.Body as MemberExpression;

            if (memberExpression == null)
            {
                throw new ArgumentException("Property expression should be of MemberExpression type!");
            }

            return memberExpression.Member.Name;
        }

        public void FirePropertyChanged(string propName)
        {
            if (this.PropertyChanged == null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }

        public void FirePropertyChanged<TReturn>(Expression<Func<TReturn>> propExpression)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(GetPropertyName(propExpression)));
            }
        }

        #region Implementation of INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

    }
}
